/*
 * @Author: wupeiwen <javapeiwen2010@gmail.com>
 * @Date: 2022-05-25 16:02:44
 * @LastEditors: wupeiwen <javapeiwen2010@gmail.com>
 * @LastEditTime: 2023-07-03 13:57:43
 * @FilePath: /tea-garden-web/src/util/storeModules/subject-pick.js
 * @Description: 专题-采摘预测
 */
import { get } from '@/util/http.js'
// import request from '@/util/request.js'

export default {
  state: {
    types: [{ name: '采摘期', value: '1' }, { name: '完成率', value: '2' }],
    activeType: '1',
    loading: {
      pickDate: false,
      weatherForcast: false,
      pickEmploymentTrend: false,
      pickDistribute: false
    },
    pickDate: null,
    weatherForcast: [],
    pickEmploymentTrend: [],
    pickDistribute: []
  },
  mutations: {
    setActiveType (state, payload) {
      state.activeType = payload
    },
    // 设置通用状态
    setCommonState (state, payload) {
      const { key, value } = payload
      state[key] = value
    },
    // 设置加载状态
    setLoadingState (state, payload) {
      const { key, value } = payload
      state.loading[key] = value
    }
  },
  actions: {
    // 采摘时间预测
    async getPickDate ({ commit, dispatch, rootState }) {
      try {
        commit('setLoadingState', { key: 'pickDate', value: true })
        commit('setCommonState', { key: 'pickDate', value: null })
        commit('setLoadingState', { key: 'weatherForcast', value: true })
        commit('setCommonState', { key: 'weatherForcast', value: [] })
        // const { data } = await get({ url: '/Pick/PickDate', params: { area_id: rootState['base-overview'].currentFarmlandId } })
        const data = {
          end_time:
          '2023-10-02',
          start_time:
          '2023-07-18',
          zh_end_time:
          '二零二三年八月十八',
          zh_start_time:
          '二零二三年六月初一'
        }
        // console.log(data)
        commit('setCommonState', { key: 'pickDate', value: data })
        dispatch('getWeatherForcast', { ...data })
      } catch (error) {
        console.log(error)
        commit('setLoadingState', { key: 'weatherForcast', value: false })
      }
      commit('setLoadingState', { key: 'pickDate', value: false })
    },
    // 天气预测
    async getWeatherForcast ({ commit }, payload) {
      try {
        commit('setLoadingState', { key: 'weatherForcast', value: true })
        // const { data } = await get({ url: '/Pick/WeatherForcast', params: { ...payload } })
        // const data = [
        //   { date: '2023-07-18', day_code: 'd01', max_temperature: 36, min_temperature: 23, night_code: 'n02', week: '周二' },
        // ]
        // const getTem = () => request.get('/week?unescape=1&appid=42961365&appsecret=S9dM2OKh&cityid=101210808')
        // const res = await getTem()
        const res = [1, 2]
        // const res = [
        //   { date: '12-07', tem_day: 20, tem_night: 4, wea_img: 'qing', wea: '晴' },
        console.log(res)
        const data = res
        // console.log(data, '我是天气')
        commit('setCommonState', { key: 'weatherForcast', value: data })
        // commit('setCommonState', { key: 'weatherForcast', value: data.reverse() })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'weatherForcast', value: false })
    },
    // 用工量分析
    async getPickEmploymentTrend ({ commit, rootState }) {
      try {
        commit('setLoadingState', { key: 'pickEmploymentTrend', value: true })
        commit('setCommonState', { key: 'pickEmploymentTrend', value: [] })
        // const { data } = await get({ url: '/Pick/PickEmploymentTrend', params: { area_id: rootState['base-overview'].currentFarmlandId } })
        const data = {
          forecast_cnt: 1438,
          plan_cnt: 29,
          trend_data: [
            { status_: 15, plan_time: '2023-09-20', cnt: 29 },
            { status_: 15, plan_time: '2023-09-15', cnt: 26 },
            { status_: 15, plan_time: '2023-09-10', cnt: 33 },
            { status_: 15, plan_time: '2023-09-05', cnt: 20 },
            { status_: 15, plan_time: '2023-08-30', cnt: 25 },
            { status_: 15, plan_time: '2023-08-25', cnt: 26 },
            { status_: 15, plan_time: '2023-08-20', cnt: 24 },
            { status_: 15, plan_time: '2023-08-15', cnt: 23 },
            { status_: 15, plan_time: '2023-08-10', cnt: 22 },
            { status_: 15, plan_time: '2023-08-05', cnt: 30 }
          ]
        }
        data.trend_data = data.trend_data.reverse().map((item) => {
          const date = item.plan_time.split('-')
          return { name: date[1] + '.' + date[2], value: item.cnt }
        })
        commit('setCommonState', { key: 'pickEmploymentTrend', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'pickEmploymentTrend', value: false })
    },
    // 采摘地块分布
    async getPickDistribute ({ commit }) {
      try {
        commit('setLoadingState', { key: 'pickDistribute', value: true })
        commit('setCommonState', { key: 'pickDistribute', value: [] })
        const { data } = await get({ url: '/Pick/PickDistribute' })
        commit('setCommonState', { key: 'pickDistribute', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'pickDistribute', value: false })
    }
  }
}
